package cn.zjyj.module.zjyj.service.risksample;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.zjyj.module.zjyj.controller.admin.risksample.vo.*;
import cn.zjyj.module.zjyj.dal.dataobject.risksample.RiskSampleDO;
import cn.zjyj.framework.common.common.pojo.PageResult;
import cn.zjyj.framework.common.common.util.object.BeanUtils;

import cn.zjyj.module.zjyj.dal.mysql.risksample.RiskSampleMapper;

import static cn.zjyj.framework.common.common.exception.util.ServiceExceptionUtil.exception;
import static cn.zjyj.framework.common.common.util.collection.CollectionUtils.convertList;
import static cn.zjyj.module.zjyj.enums.ErrorCodeConstants.*;

/**
 * 风险样本 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class RiskSampleServiceImpl implements RiskSampleService {

    @Resource
    private RiskSampleMapper riskSampleMapper;

    @Override
    public Long createRiskSample(RiskSampleSaveReqVO createReqVO) {
        // 插入
        RiskSampleDO riskSample = BeanUtils.toBean(createReqVO, RiskSampleDO.class);
        riskSampleMapper.insert(riskSample);
        // 返回
        return riskSample.getId();
    }

    @Override
    public void updateRiskSample(RiskSampleSaveReqVO updateReqVO) {
        // 校验存在
        validateRiskSampleExists(updateReqVO.getId());
        // 更新
        RiskSampleDO updateObj = BeanUtils.toBean(updateReqVO, RiskSampleDO.class);
        riskSampleMapper.updateById(updateObj);
    }

    @Override
    public void deleteRiskSample(Long id) {
        // 校验存在
        validateRiskSampleExists(id);
        // 删除
        riskSampleMapper.deleteById(id);
    }

    @Override
        public void deleteRiskSampleListByIds(List<Long> ids) {
        // 校验存在
        validateRiskSampleExists(ids);
        // 删除
        riskSampleMapper.deleteByIds(ids);
        }

    private void validateRiskSampleExists(List<Long> ids) {
        List<RiskSampleDO> list = riskSampleMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(RISK_SAMPLE_NOT_EXISTS);
        }
    }

    private void validateRiskSampleExists(Long id) {
        if (riskSampleMapper.selectById(id) == null) {
            throw exception(RISK_SAMPLE_NOT_EXISTS);
        }
    }

    @Override
    public RiskSampleDO getRiskSample(Long id) {
        return riskSampleMapper.selectById(id);
    }

    @Override
    public PageResult<RiskSampleDO> getRiskSamplePage(RiskSamplePageReqVO pageReqVO) {
        return riskSampleMapper.selectPage(pageReqVO);
    }

}